package com.neepu.controller;

import com.neepu.pojo.MessageBoard;
import com.neepu.pojo.User;
import com.neepu.service.MessageBoardService;
import com.neepu.util.annotation.ArchivesLog;
import com.neepu.util.common.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class MessageBoardController {

    @Autowired
    private MessageBoardService messageBoardService;

    @ArchivesLog(operteContent = "查看留言板")
    @RequestMapping("/message")
    public ModelAndView messageboard(ModelAndView mv){
        List<MessageBoard> list = messageBoardService.getAllMessage();
        mv.addObject("list",list);
        mv.setViewName("messageboard/message_board");
        return mv;
    }

//  向数据库中插入一条留言
    @ArchivesLog(operteContent = "添加留言")
    @RequestMapping("/postMessage")
    public ModelAndView postMessage(String content, HttpSession session,ModelAndView mv){
        User user = (User) session.getAttribute(Constants.USER_SESSION);
        MessageBoard messageBoard = new MessageBoard();
        messageBoard.setContent(content);
        // 添加用户的ID
        messageBoard.setPerson(user.getUserName());
        messageBoard.setAvatar(user.getAvatar());
        messageBoardService.addMessageBoard(messageBoard);
        mv.setViewName("forward:message");
        mv.addObject("message","留言成功！");
        return mv;
    }
}
